<?php
/**
 * Created by PhpStorm.
 * User: LMtwo
 * Date: 2019/7/1
 * Time: 12:57
 */

namespace app\stage\controller;
use think\Controller;
use think\Db;
use app\stage\controller\WX_Class;
class Weixin extends Controller
{
   public function index(){
       $weixin = new WX_Class();
       if (!$msg = $weixin->msg()){
           die();
       }
      die();
       //文本消息
       if ((string)$msg->MsgType == 'text') {
           $content = (string)$msg->Content;
       }
       //语音消息
       if ((string)$msg->MsgType == 'voice') {
           $content = (string)$msg->Recognition;
       }
       //图片消息
       if ((string)$msg->MsgType == 'image') {
           $PicUrl = (string)$msg->PicUrl;
           $result = WenZhi($PicUrl);
           if($result['words_result_num']>0){
               $content = '';
               for ($i=0;$i<$result['words_result_num'];$i++){
                   $content.= $result['words_result'][$i]['words'];
               }
           }

       }

       //查题开始
       $formUser = (string)$msg->FromUserName;
       $code = Db::name('php_member0')->where(['openid' => $formUser])->value('code');
       if($code<1){
           $weixin->send('news', array(
               'to'      => (string)$msg->FromUserName,
               'from'    => (string)$msg->ToUserName,
               'time'    => time(),
               'items' => [
                   'title' => '你的查题次数已用完',
                   'description' => '为了获取更好的服务，点击打赏1元获取更多查询次数！售后客服微信：famaji1 ',
                   'picurl' => WEB_URL.'/static/xcxImage/myself.jpg',
                   'url' => WEB_URL.'/stage/shop?aid=0&openid='.$msg->FromUserName
               ],
           ));
           exit;
       }
       $title = str_replace(' ','maokegang',$content);
       $data = file_get_contents("https://599595.cn/Xcx_One.php?title=".$title);
       $data = json_decode($data,true);
       if($data['code'] == 1){
           Db::name('php_member0')->where(['openid' => $formUser])->setDec('code',1);
           $replay = trim("🔒：".strip_tags($data['title']))."\n\n🔑：".str_replace("#","\n\n",$data['answer'].'');

       }else{

           $replay = '暂未找到,换个关键词试试！';
       }


       $weixin->send('text', array(
           'to'      => (string)$msg->FromUserName,
           'from'    => (string)$msg->ToUserName,
           'time'    => time(),
           'content' => $replay,
       ));

   }
}